package cn.innosmart.aq.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mobeta.android.dslv.DragSortListView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.RuleActionDragSortListViewAdapter;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.bean.CommandBean;
import cn.innosmart.aq.bean.RuleBean;
import cn.innosmart.aq.util.SystemConstant;
import cn.innosmart.aq.view.activity.AddActionActivity;
import cn.innosmart.aq.view.activity.RuleActivity;

/**
 * Created by Woody on 2015/4/3.
 */
public class RuleStep2Fragment extends Fragment {
    private View view;
    private DragSortListView dslvAction;
    private RuleActionDragSortListViewAdapter ruleActionDragSortListViewAdapter;
    private ArrayList<String> actions;
    private AquariumBean aquariumBean;
    private RuleBean ruleBean;
    private TextView tvAddInfo;
    private Menu menus;
    /**
     * 设置Toolbar
     */
    private Toolbar toolbar;
    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.rule_add_action:
                    //添加Action
                    Intent intent = new Intent(getActivity(), AddActionActivity.class);
                    intent.putExtra("flag", "rule");
                    intent.putExtra("aquarium", aquariumBean.getId());
                    intent.putStringArrayListExtra("actions", actions);
                    startActivity(intent);
                    break;
            }
            return true;
        }

    };
    //监听器在手机拖动停下的时候触发
    private DragSortListView.DropListener onDrop =
            new DragSortListView.DropListener() {
                @Override
                public void drop(int from, int to) {//from to 分别表示 被拖动控件原位置 和目标位置
                    if (from != to) {
                        CommandBean item = (CommandBean) ruleActionDragSortListViewAdapter.getItem(from);//得到listview的适配器
                        ruleActionDragSortListViewAdapter.remove(from);//在适配器中”原位置“的数据。
                        ruleActionDragSortListViewAdapter.insert(item, to);//在目标位置中插入被拖动的控件。
                    }
                }
            };
    //删除监听器，点击左边差号就触发。删除item操作。
    private DragSortListView.RemoveListener onRemove =
            new DragSortListView.RemoveListener() {
                @Override
                public void remove(int which) {
                    ruleActionDragSortListViewAdapter.remove(which);
                }
            };

    public static RuleStep2Fragment newInstance(AquariumBean aquariumBean) {
        RuleStep2Fragment f = new RuleStep2Fragment();
        Bundle args = new Bundle();
        args.putParcelable("aquarium", aquariumBean);
        f.setArguments(args);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_rule_step2, container, false);
        tvAddInfo = (TextView) view.findViewById(R.id.tv_add_info);
        aquariumBean = SystemConstant.currentAquariumBean;
        actions = new ArrayList<String>();
        actions.addAll((ArrayList<String>) aquariumBean.getActionDevices().clone());
        ruleBean = RuleActivity.ruleBean;
        System.out.println("triigers.size" + actions.size());
        ruleActionDragSortListViewAdapter = new RuleActionDragSortListViewAdapter(getActivity(), ruleBean.getCommands());
        dslvAction = (DragSortListView) view.findViewById(R.id.dslv_action);
        dslvAction.setRemoveListener(onRemove);
        dslvAction.setDropListener(onDrop);
        dslvAction.setAdapter(ruleActionDragSortListViewAdapter);
        dslvAction.setDragEnabled(true);
        setHasOptionsMenu(true);
        setBar();
        return view;
    }

    private void setBar() {
        toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        getActivity().getMenuInflater().inflate(R.menu.menu_rule_add_action, menu);
        menus = menu;
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onResume() {
        if (ruleBean.getCommands() != null)
            ruleActionDragSortListViewAdapter.update(ruleBean.getCommands());
        if (ruleBean.getCommands() == null || ruleBean.getCommands().size() == 0) {
            tvAddInfo.setVisibility(View.VISIBLE);
        } else {
            tvAddInfo.setVisibility(View.GONE);
        }
        super.onResume();
    }
}
